package com.ourchem.bus.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ourchem.bus.domain.LegalStatusJson;
import com.ourchem.bus.domain.Patent;
import com.ourchem.bus.feign.ProDataFeignService;
import com.ourchem.bus.service.IProDataService;
import com.ourchem.common.utils.StringUtils;
import com.ourchem.data.api.file.FileCollection;
import com.ourchem.data.api.file.PatentCollection;
import com.ourchem.data.api.query.QueryColumn;
import com.ourchem.data.api.query.QueryParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 *   从数据中心查询专利数据
 * */
@Service
public class ProDataServiceImpl implements IProDataService {

    @Autowired
    private ProDataFeignService proDataFeignService;

    @Override
    public List<Patent> search(String expression) {
        if(StringUtils.isBlank(expression)){
            return null;
        }
        List<String> alias = new ArrayList<>();
        alias.add("title");
        alias.add("application_num_sear");
        alias.add("publication_number_sear");
        alias.add("patent_duration");
        alias.add("applicants_stat");
        alias.add("inventors");
        alias.add("assignees");
        alias.add("application_date");
        alias.add("publication_date");
        alias.add("priority_date");
        alias.add("priority_num");
        alias.add("abstract_ab");
        alias.add("patent_value_score");
        alias.add("applicants_normal_stat");

        alias.add("ipc_num");
        alias.add("claims");
        alias.add("description");
        alias.add("legal_event_status_json");
        alias.add("applicant_addr");
        alias.add("agent");
        alias.add("agency");
        alias.add("patent_status");

        QueryParam queryParam = encapsulationEntity(1, expression, 0, 1000, alias);
        String search = proDataFeignService.search(queryParam);
        JSONObject jsonObject = JSON.parseObject(search);
        JSONArray result = jsonObject.getJSONArray("result");
        if (result.size() == 0) {
            return null;
        }
        List<Patent> list = new ArrayList<>();
        for(int i = 0; i < result.size(); i++){
            JSONObject object = result.getJSONObject(i);
            Patent patent = JSON.toJavaObject(object, Patent.class);
            if(patent != null){
                String[] legalEvents = patent.getLegal_event_status_json();
                LegalStatusJson[] legalStatusJsons = convertToLegalStatus(legalEvents);
                patent.setLegalEventStatus(legalStatusJsons);
            }
            list.add(patent);

        }
        return list;
    }

    private LegalStatusJson[] convertToLegalStatus(String[] legalEvents) {
        if(legalEvents == null){
            return null;
        }
        LegalStatusJson[] legalStatusJsons = new LegalStatusJson[legalEvents.length];
        for(int i = 0; i < legalEvents.length; i++){
            JSONObject legalStatus = JSON.parseObject(legalEvents[i]);
            LegalStatusJson legalStatusJson = JSON.toJavaObject(legalStatus, LegalStatusJson.class);
            legalStatusJsons[i] = legalStatusJson;
        }
        return legalStatusJsons;
    }


    //封装请求对象
    private QueryParam encapsulationEntity(Integer expreType, String searchExpre, Integer Offset, Integer pageSize, List<String> Alias) {
        QueryParam queryParam = new QueryParam(); //
        queryParam.setExpreType(expreType); //
        queryParam.setModelId("75c3b76f-1cb9-11eb-b1c6-70106fc9fcee");//
        queryParam.setSearchExpre(searchExpre);
        queryParam.setOffset(Offset);
        queryParam.setPageSize(pageSize);
        ArrayList<QueryColumn> list = new ArrayList<>();
        for (String Alia : Alias) {
            QueryColumn queryColumn = new QueryColumn();
            queryColumn.setAlias(Alia);
            list.add(queryColumn);
        }
        queryParam.setRequestCols(list);
        return queryParam;
    }


    /**
     * @author:
     * @description: 获取数据中心图片地址
     * @params:
     * @return: 图片的list
     */
    @Override
    public List<FileCollection> getPictures(List<String> publicNumbers) {
        if (publicNumbers == null || publicNumbers.size() == 0) {
            return null;
        }

        PatentCollection collection = new PatentCollection();
        collection.setPublicNums(publicNumbers);
        collection.setFileType(4);
        collection.setInternal(false);
        String pics = proDataFeignService.getPatentPics(collection);
        JSONArray result = JSON.parseArray(pics);
        if(result == null || result.size() == 0) {
            return null;
        }
        List<FileCollection> list = new ArrayList<>();
        for(int i = 0; i < result.size(); i++) {
            JSONObject object = result.getJSONObject(i);
            FileCollection fileCollection = JSON.toJavaObject(object, FileCollection.class);
            list.add(fileCollection);
        }
        return list;
    }
}
